/*
  用户管理接口
*/

import axios from "axios";

// 1. 分页查询users
// reqGetUsersList返回值是axios函数的返回值，promise
// promise对象是成功状态：说明请求成功了并有响应的数据
// promise对象是失败状态：说明请求失败了并有失败的原因
export const reqGetUsersList = (page, limit) => {
  return axios({
    method: "GET",
    url: "http://localhost:3000/users",
    params: {
      _page: page,
      _limit: limit,
    },
  });
};

export const reqGetAllUsers = () => {
  return axios({
    method: "GET",
    url: "http://localhost:3000/users",
  });
};

export const reqGetUser = (id) => {
  return axios({
    method: "GET",
    url: `http://localhost:3000/users/${id}`,
  });
};
// 2. 添加user
/*
  user: {
    username,
    nickname,
    password,
    createdTime,
    updatedTime
  }
*/
export const reqSaveUser = (user) => {
  return axios({
    method: "POST",
    url: "http://localhost:3000/users",
    data: {
      ...user,
      createdTime: Date.now(),
      updatedTime: Date.now(),
    },
  });
};

// 3. 更新user
export const reqUpdateUser = (user) => {
  return axios({
    method: "PATCH",
    url: `http://localhost:3000/users/${user.id}`,
    data: {
      nickname: user.nickname,
      password: user.password,
      updatedTime: Date.now(),
    },
  });
};

// 4. 删除user
export const reqDeleteUser = (id) => {
  return axios({
    method: "DELETE",
    url: `http://localhost:3000/users/${id}`,
  });
};
